package com.burgeon.backend.EnterpriseWeChat.rest;

import com.burgeon.backend.EnterpriseWeChat.entity.SaleData;
import com.burgeon.backend.EnterpriseWeChat.entity.SaleStoreData;
import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindSalerData extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindSalerData(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     *
     * @return
     */

    public SaleData getData(String phone) throws Exception {
        String url = "http://" + ip + "/servlets/binserv/Rest";

        String str = RESULT(Data(phone), url);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        if (!"0".equals(code)) {
            throw new Exception();
        }

        JSONArray jsonArray = new JSONArray(str).getJSONObject(0).getJSONArray("rows");
        SaleData saleStoreData = new SaleData();
        if(jsonArray.length()>0){
            saleStoreData.setSalerName(jsonArray.getJSONArray(0).getString(0));
            saleStoreData.setTotSalerPrice(jsonArray.getJSONArray(0).getString(1));
            saleStoreData.setnVipSaler(jsonArray.getJSONArray(0).getString(2));
            saleStoreData.setTB(jsonArray.getJSONArray(0).getString(3));
            saleStoreData.setHB(jsonArray.getJSONArray(0).getString(4));
            saleStoreData.setSalerRate(jsonArray.getJSONArray(0).getString(5));
            saleStoreData.setTotSalerMark(jsonArray.getJSONArray(0).getString(6));

            saleStoreData.setTotVipSalerQty(jsonArray.getJSONArray(0).getString(7));
            saleStoreData.setTotSalerVipPrice(jsonArray.getJSONArray(0).getString(8));
            saleStoreData.setVipSalerMoney(jsonArray.getJSONArray(0).getString(9));

            saleStoreData.setSaleSrepId(jsonArray.getJSONArray(0).getString(10));


        }

        return saleStoreData;
    }

    private MultiValueMap Data(String phone) {
        HashMap params = new HashMap();
        ArrayList colums = new ArrayList();//查询字段

        colums.add("'SALESREP_ID;NAME'");//导购
        colums.add("TOT_SALER_PRICE");//导购销售金额
        colums.add("N_VIPSALER");//导购新增会员数
        colums.add("TB");//同比
        colums.add("HB");//环比
        colums.add("SALER_RATE");//导购达成率
        colums.add("TOT_SALER_MARK");//导购指标


        colums.add("TOT_VIPSALER_QTY");//导购总会员数
        colums.add("TOT_SALERVIP_PRICE");//导购会销
        colums.add("VIP_SALERMONEY");//导购储值金额


        colums.add("SALESREP_ID");//导购


//        HashMap condition = new HashMap();
//        condition["column"] = "PHONE";
//        condition["condition"] = phone;


        params.put("columns", colums);
        params.put("table", "S_BULLETINBOARD");//表名

        params.put("count", "true");

        HashMap paramss = new HashMap();
        paramss.put("column", "'PHONE'");
        paramss.put("condition", phone);

        params.put("params", paramss);

        return DATA("Query", params, account, password);
    }

    public static void main(String[] args) {
        FindSalerData g = new FindSalerData("47.92.53.75:90", "dy@xcx.com", "123");
        try {
            System.err.println(g.getData("17732141036"));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
